﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Downloader.Domain;
using Downloader.Domain.Contracts.Tasks;
using Downloader.Framework.Queries;
using Downloader.Infrastructure;
using Downloader.Web.Controllers.Queries;
using Downloader.Web.Controllers.ViewModels;
using SharpArch.NHibernate.Web.Mvc;

namespace Downloader.Web.Controllers.Admin
{
    public class CategoryController : Controller
    {
        private readonly ICategoryTasks _categoryTasks = null;

        public ICategoryListQuery CategoryListQuery { get; set; }
        public CategoryController(ICategoryTasks tasks)
        {
            this._categoryTasks = tasks;
        }
        [Authorize]
        [Transaction]
        public ActionResult Index()
        {
            ViewData["Category"] = new SelectList(CategoryListQuery.GetMenu(), "Id", "Name");    
            return View();
        }

        public ActionResult List(int index,int size)
        {
            var resultList = new List<CategoryViewModel>();
            var tempList = CategoryListQuery.GetList(index, size);
            foreach (var t in tempList)
            {
                resultList.Add(t);
                var subList = CategoryListQuery.GetSubList(t.Id);
                resultList.AddRange(subList);
            }
             var firstResult = (index - 1) * size;
            RecordList<CategoryViewModel> recordList=new RecordList<CategoryViewModel>();
            recordList.Data = resultList.Skip(firstResult).Take(size).ToList();
            recordList.PageIndex = index;
            recordList.PageSize = size;
            recordList.RecordCount = resultList.Count;

            return PartialView(recordList);
        }

        public ActionResult SubList(int parentId)
        {
            var result = CategoryListQuery.GetSubList(parentId);
            return PartialView(result);
        }

        [Transaction]
        public ActionResult SaveOrUpdate(Category category)
        {
            try
            {
                _categoryTasks.CreateOrUpdate(category);
            }
            catch (Exception exc)
            {
                 return Json(new { success = false, data = exc.Message });
            }
            return Json(new {success = true});
        }

        public ActionResult Edit(int id)
        {
            var model = _categoryTasks.Get(id);
            var viewModel=new CategoryViewModel(model);
            return Json(new {data=viewModel}, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加子分类
        /// </summary>
        /// <param name="id">父分类ID</param>
        /// <returns></returns>
        public ActionResult AddSubCategory(int id)
        {
            ViewData["Category"] = new SelectList(CategoryListQuery.GetMenu(), "Id", "Name");   
            var model = _categoryTasks.Get(id);
            return View(model);
        }


        [Transaction]
        public ActionResult Dele(int id)
        {
            try
            {
                _categoryTasks.Delete(id);
                return Json(new {success = true});
            }catch(Exception e)
            {
                return Json(new {success = false, data = e.Message});
            }
        }

    }
}